package com.qikecn.utils;

import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Set;

/**
 * 全局方法工具类
 *
 */
public class Jeff_GlobalUtil {
	
	// Create an 24 byte UUID
	protected static int count = 0;
	
	public static double getDistatce(double lat1, double lat2, double lon1,    double lon2) { 
        double R = 6378.137; 
        double distance = 0.0; 
        double dLat = (lat2 - lat1) * Math.PI / 180; 
        double dLon = (lon2 - lon1) * Math.PI / 180; 
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) 
                + Math.cos(lat1 * Math.PI / 180) 
                * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon / 2) 
                * Math.sin(dLon / 2); 
        distance = (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))) * R; 
        return distance; 
    }

	/**
	 * 生成24位UUID
	 * @return UUID 24bit string
	 */
	public static synchronized String getUUID() {
		count++;
		long time = System.currentTimeMillis();

		String uuid = "G" + Long.toHexString(time) + Integer.toHexString(count)
				+ Long.toHexString(Double.doubleToLongBits(Math.random()));

		uuid = uuid.substring(0, 24).toUpperCase();

		return uuid;
	}

	/**
	 * 盐巴密码加密算法
	 * @param userCode  用户名
	 * @param password  密码
	 * @return 加密后字符串
	 */
	@SuppressWarnings("static-access")
	public static String encryptPassword(String userCode, String password) {
		MD5Util md5 = new MD5Util();
		return md5.md5(userCode + "@yale#" + password);
	}

	/**
	 * null值的改变
	 * @param in 指定字符串
	 * @return 如果指定字符串为null,返回"",否则返回本身
	 */
	public static String chgNull(String in) {
		String out = null;
		out = in;
		if (out == null || (out.trim()).equals("null")) {
			return "";
		} else {
			return out.trim();
		}
	}

	/**
	 * double类型取小数点后面几位
	 * @param val 指定double型数字
	 * @param precision 取前几位
	 * @return 转换后的double数字
	 */
	public static Double roundDouble(double val, int precision) {
		Double ret = null;
		try {
			double factor = Math.pow(10, precision);
			ret = Math.floor(val * factor + 0.5) / factor;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ret;
	}

	/**
	 * 数组转换成字符串
	 * @param values 字符串数组
	 * @return 以逗号隔开的字符串
	 */
	public static String Array2String(String[] values) {
		String result = "";
		if (values == null) {
			return result;
		}
		int len = values.length;
		for (int i = 0; i < len; i++) {
			result += values[i] + ",";
		}
		if (result.endsWith(",")) {
			result = result.substring(result.length() - 1);
		}
		return result;
	}

	/**
	 * 数组转换成字符串
	 * @param values 对象串数组
	 * @return 以逗号隔开的字符串
	 */
	public static String Array2String(Object[] values) {
		String result = "";
		if (values == null) {
			return result;
		}
		int len = values.length;
		for (int i = 0; i < len; i++) {
			result += values[i].toString() + ",";
		}
		if (result.endsWith(",")) {
			result = result.substring(result.length() - 1);
		}
		return result;
	}

	/**
	 * 字符转换成数字，如果字符格式错误返回-1
	 * @param str 需要输入的字符串
	 * @return 格式错误:返回为-1   正确:返回整数
	 */
	public static int str2int(String str) {
		int result = 0;
		try {
			result = Integer.parseInt(str);
		} catch (Exception e) {
			result = -1;
		}
		return result;
	}

	/**
	 * 数字转换成字符串
	 * @param int 需要输入的整型数
	 * @return 转换后的字符串
	 */
	public static String int2str(int i) {
		String result = "";
		result = String.valueOf(i);
		return result;
	}

	/**
	 * 
	 * 功能 ：UTF-8转换为GBK
	 * @param strUtf8
	 * @return	GBK
	 * @throws UnsupportedEncodingException
	 */
	public static String UTF8toGBK(String strUtf8)
			throws UnsupportedEncodingException {

		String encode = "";
		encode = new String(strUtf8.trim().getBytes("UTF-8"), "GBk");
		return encode;
	}

	/**
	 * 
	 * 功能 ：UTF-8转换为ISO-8859-1
	 * @param strUtf8
	 * @return ISO-8859-1
	 * @throws UnsupportedEncodingException
	 */
	public static String UTF8toISO88591(String strUtf8)
			throws UnsupportedEncodingException {

		String encode = "";
		encode = new String(strUtf8.trim().getBytes("UTF-8"), "ISO-8859-1");
		return encode;
	}

	/**
	 * 
	 * 功能 ：GBK转换为UTF-8
	 * @param strGbk
	 * @return UTF-8
	 * @throws UnsupportedEncodingException
	 */
	public static String GBKtoUTF8(String strGbk)
			throws UnsupportedEncodingException {

		String encode = "";
		encode = new String(strGbk.trim().getBytes("GBk"), "UTF-8");
		return encode;
	}

	/**
	 * 
	 * 功能 ：GBK转换为ISO-8859-1
	 * @param strGbk
	 * @return ISO-8859-1
	 * @throws UnsupportedEncodingException
	 */
	public static String GBKtoISO88591(String strGbk)
			throws UnsupportedEncodingException {

		String encode = "";
		encode = new String(strGbk.trim().getBytes("GBk"), "ISO-8859-1");
		return encode;
	}

	/**
	 * 
	 * 功能 ：ISO-8859-1转换为GBK
	 * @param strISO88591
	 * @return GBK
	 * @throws UnsupportedEncodingException
	 */
	public static String ISO88591toGBK(String strISO88591)
			throws UnsupportedEncodingException {

		String encode = "";
		encode = new String(strISO88591.trim().getBytes("ISO-8859-1"), "GBK");
		return encode;
	}

	/**
	 * 
	 * 功能 ：ISO-8859-1转换为UTF-8
	 * @param strISO88591
	 * @return UTF-8
	 * @throws UnsupportedEncodingException
	 */
	public static String ISO88591toUTF8(String strISO88591)
			throws UnsupportedEncodingException {

		String encode = "";
		encode = new String(strISO88591.trim().getBytes("ISO-8859-1"), "UTF-8");
		return encode;
	}
	/****
	 * 
	 * @param typeId
	 * @return 返回用户性别
	 */
	public static String sexTypeName(int typeId){
		String returnName = null;
		switch (typeId) {
		case 0:
			returnName = "男";
			break;
		case 1:
			returnName = "女";
			break;
		}
		return returnName;
	}
	
	public static boolean ifImage(String fileName){
		 boolean ifImage = false;
		 String ex = getExtention(fileName);
		 if(ex.equalsIgnoreCase("gif") || ex.equalsIgnoreCase("jpg") || ex.equalsIgnoreCase("jpeg") || ex.equalsIgnoreCase("png")){
			 ifImage = true;
		 }
		
		return ifImage;
	}
	
	public static String getExtention(String fileName)  {
    	int length = fileName.length();
        int pos = fileName.lastIndexOf(".");
        if(fileName.equals("")||pos<0||length==pos){
        	return "unknow";
        }
        return fileName.substring(pos+1);
   }
	
	/**
	 * 生成与原set完全不一样的固定长度新set
	 * 
	 * @param oldSet
	 * @param num
	 */
	public static Set<String> cardNumGenerator(Set<String> oldSet, int num) {
		if (null == oldSet) {
			oldSet = new HashSet<String>();
		}
		Set<String> newSet = new HashSet<String>();
		for (String old : oldSet) {
			newSet.add(old);
		}
		while (newSet.size() < oldSet.size() + num) {
			String str = "";
			for (int i = 0; i < 10; i++) {
				str += (int) (Math.random() * 10);
			}
			newSet.add(str);
		}
		for (String old : oldSet) {
			newSet.remove(old);
		}
		return newSet;
	}
	
}
